def calculate_pi(iterations):
    pi = 0
    sign = 1  # 符号，用于交替加减
    for i in range(iterations):
        term = 1 / (2 * i + 1)  # 计算每一项的值
        pi += sign * term  # 根据符号添加或减去当前项
        sign *= -1  # 切换符号
    return 4 * pi  # 返回圆周率的近似值

# 设置迭代次数
iterations = 10000  # 你可以根据需要调整这个值

# 计算圆周率
approx_pi = calculate_pi(iterations)

# 打印结果
print(f"近似的圆周率值为: {approx_pi}")
